package com.eric.windows.actionbar;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.eric.windows.R;

public class ActionFragment2 extends Fragment {
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setHasOptionsMenu(true);//通知Activity，当前Fragment也有菜单
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_action_test, container, false);
		view.findViewById(R.id.txt_fragment_action).setBackgroundColor(Color.RED);
		return view;
	}
	/**
	 * Fragment也可以有自己的菜单，但是需要Activity来帮助加载
	 */
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		inflater.inflate(R.menu.fragment_menu, menu);
	}
	/**
	 * 线执行Activity中的onOptionsItemSelected，然后再执行Fragment中的方法。
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Toast.makeText(getActivity(), "Fragemtn的菜单被执行", 0).show();
		return super.onOptionsItemSelected(item);
	}

}
